﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace Rio.Framework.Pages.AppCode.ApplicationNavigation
{
    public class RecyclingContentLoader : INavigationContentLoader
    {
        #region Fields

        private UserControl _currentView;
        private bool _isNavigatingToSameView;
        private PageResourceContentLoader _loader = new PageResourceContentLoader();

        #endregion

        public IAsyncResult BeginLoad(Uri targetUri,
                                      Uri currentUri,
                                      AsyncCallback userCallback,
                                      object asyncState)
        {
            return _loader.BeginLoad(targetUri,
                                     currentUri,
                                     userCallback,
                                     asyncState);
        }

        public bool CanLoad(Uri targetUri, Uri currentUri)
        {
            return _loader.CanLoad(targetUri, currentUri);
        }

        public void CancelLoad(IAsyncResult asyncResult)
        {
            _loader.CancelLoad(asyncResult);
        }

        public LoadResult EndLoad(IAsyncResult asyncResult)
        {
            //if (_isNavigatingToSameView)
            //{
            //    return new LoadResult(_currentView);
            //}

            var loadResult = _loader.EndLoad(asyncResult);

            _currentView = loadResult.LoadedContent as UserControl;
            return loadResult;
        }
    }
}